/**
Copyright (c) 2013 cnova

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

library JDummy requires JWidget

    struct JDummy extends JWidget
        private static constant real defaultSize = 100.0
        
        private real array sizes[12]
        private unit array dummies[12]
        private real array facing[12]
        private real array heights[12]
        
        static method create takes nothing returns thistype
            local thistype this = .allocate()
            local integer i = 0
            loop
                exitwhen i > 12
                set .dummies[i] = null
                set i = i + 1   
            endloop
            set i = 0
            loop
                exitwhen i > 12
                set .sizes[i] = thistype.defaultSize
                set i = i + 1   
            endloop
            set i = 0
            loop
                exitwhen i > 12
                set .facing[i] = 270.0
                set i = i + 1   
            endloop
            return this
        endmethod
        method setAngle takes integer angle returns nothing
            local integer i = 0
            loop
                exitwhen i > 12
                if .dummies[i] != null then
                    set .facing[i] = I2R(angle-180)
                    call SetUnitFacing(.dummies[i], .facing[i])
                endif
                set i = i + 1   
            endloop
        endmethod
        method setTypeId takes integer unittypeid, player p returns nothing
            local unit u
            local real s
            local integer i
            local boolean change = true
            if p != null then
                set i = GetPlayerId(p)
                set u = .dummies[i]
                if u != null and GetUnitTypeId(u) == unittypeid then
                    set change = false
                elseif u != null then
                    call RemoveUnit(u)
                endif
                if unittypeid != 0 and change then
                    set u = CreateUnit(p, unittypeid, .x, .y, .facing[i])
                    call SetUnitFlyHeight(u, 70.0, 1000.0)
                    call SetUnitPathing(u, false)
                    call UnitAddAbility(u, 'Aloc')
                    call SetUnitInvulnerable(u, true)
                    set s = .sizes[i]/100.0
                    call SetUnitScale(u,s,s,s)
                    if GetLocalPlayer() == p then
                        call SetUnitVertexColor(u, 255,255,255,255)
                    else
                        call SetUnitVertexColor(u, 255,255,255,0)
                    endif
                    set .dummies[i] = u
                endif
            endif
            set u = null
        endmethod
        method setHeight takes integer height, player p returns nothing
            local integer i = 0
            local unit u
            local real s
            if p != null then
                set i = GetPlayerId(p)
                set .heights[i] = I2R(height)
                set u = .dummies[i]
                if u != null then
                    call SetUnitY(u, .x + .heights[i])
                endif
            else
                loop
                    exitwhen i > 12
                    set .heights[i] = I2R(height)
                    set u = .dummies[i]
                    if u != null then
                        call SetUnitY(u, .x + .heights[i])
                    endif
                    set i = i + 1   
                endloop
            endif
            set u = null
        endmethod
        method setSize takes integer size, player p returns nothing
            local integer i = 0
            local unit u
            local real s
            if p != null then
                set i = GetPlayerId(p)
                set .sizes[i] = I2R(size)
                set u = .dummies[i]
                if u != null then
                    set s = .sizes[i]/100.0
                    call SetUnitScale(u,s,s,s)
                endif
            else
                loop
                    exitwhen i > 12
                    set .sizes[i] = I2R(size)
                    set u = .dummies[i]
                    if u != null then
                        set s = .sizes[i]/100.0
                        call SetUnitScale(u,s,s,s)
                    endif
                    set i = i + 1   
                endloop
            endif
            set u = null
        endmethod
        stub method draw takes nothing returns nothing
            local integer i = 0
            loop
                exitwhen i > 12
                if .dummies[i] != null then
                    call ShowUnit(.dummies[i], true)
                    call SetUnitX(.dummies[i],.x)
                    call SetUnitY(.dummies[i],.y) 
                endif
                set i = i + 1   
            endloop
        endmethod
        stub method hide takes nothing returns nothing
            local integer i = 0
            loop
                exitwhen i > 12
                if .dummies[i] != null then
                    call ShowUnit(.dummies[i], false)
                endif
                set i = i + 1   
            endloop
        endmethod
        
        private integer array counters[12]
        private method setAnimationT takes string animation, real delay, player p, integer i returns nothing
            if delay != 0 then
                call TriggerSleepAction(delay)
            endif
            if .dummies[GetPlayerId(p)] != null and i == .counters[GetPlayerId(p)]  then
                call SetUnitAnimation(.dummies[GetPlayerId(p)], animation)
            endif
        endmethod
        method setAnimation takes string animation, real delay, player p returns nothing
            set .counters[GetPlayerId(p)] = .counters[GetPlayerId(p)] + 1
            call .setAnimationT.execute(animation, delay, p, .counters[GetPlayerId(p)] )
        endmethod
        
        method onDestroy takes nothing returns nothing
            local integer i = 0
            loop
                exitwhen i > 12
                if .dummies[i] != null then
                    call RemoveUnit(.dummies[i])
                    set .dummies[i] = null
                endif
                set i = i + 1   
            endloop
        endmethod
    endstruct
endlibrary